<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Meter_tracking extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('PHPexcel');

	}

	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array('zone1' => $this->meter_tracking_model->getzoning('ZONE1'),
	  															   'zone' => $this->meter_tracking_model->getzoning2('ZONE1')));
	  $this->load->view('layouts/footer');
	 }

	 public function add_form(){
		$this->load->view($this->session->userdata('foradd'));
	}

	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'));
	}

	public function view_form($id){
		$this->load->view($this->session->userdata('forview'));
	}

	public function get_status() {
		$data = array('account_number'	=>	$this->input->post('account_number'),
					  'account_name'	=>	$this->input->post('account_name'),
					  'install_date'	=>	$this->input->post('install_date'),
					  'meter_serial'	=>	$this->input->post('meter_serial'),
					  'area'			=>	$this->input->post('area'),
					  'month_used'		=>	$this->input->post('month_used'));
		$q = $this->meter_tracking_model->getData($data);
		echo json_encode($q);
	}

	public function gen_excel() {
		$data = array('account_number'	=>	$_GET['account_number'],
					  'account_name'	=>	$_GET['account_name'],
					  'install_date'	=>	$_GET['install_date'],
					  'meter_serial'	=>	$_GET['meter_serial'],
					  'area'			=>	$_GET['area'],
					  'month_used'		=>	$_GET['month_used']);
		$q = $this->meter_tracking_model->getData($data);
		$excel = new PHPExcel();
		$excel->setActiveSheetIndex(0);
		$excel->getActiveSheet()->setTitle('Meter Tracking Report');
		$excel->getActiveSheet()->setCellValue('A1', 'Meter Tracking Report');
		$excel->getActiveSheet()->setCellValue('A3', 'Area');
		$excel->getActiveSheet()->setCellValue('B3', 'Customer');
		$excel->getActiveSheet()->setCellValue('C3', 'Meter Serial');
		$excel->getActiveSheet()->setCellValue('D3', 'Installed Date');
		$excel->getActiveSheet()->setCellValue('E3', 'Months Used');
		$cellnum = 4;
		//var_dump($q);

		foreach ($q as $key) {
			$total = 0;
			$x = $cellnum;
			$excel->getActiveSheet()->setCellValue('A' . $cellnum, $key->meaning);
			$excel->getActiveSheet()->setCellValue('B' . $cellnum, $key->account_name);
			$excel->getActiveSheet()->setCellValue('C' . $cellnum, $key->serial_number);
			$excel->getActiveSheet()->setCellValue('D' . $cellnum, $key->install_date);
			$excel->getActiveSheet()->setCellValue('E' . $cellnum, $key->month_used);
			++$cellnum;
		}

		$excel->getActiveSheet()->getStyle("A3:E3")->getFont()->setBold(true);
		$excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
		$excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
		$excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
		//change the font size
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(12);
		//make the font become bold
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$excel->getActiveSheet()->mergeCells('A1:E1');
		//set aligment to center for that merged cell (A1 to D1)
		$excel->getActiveSheet()->getStyle('A3:E3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//$excel->getActiveSheet()->getStyle("A2:F2")->getFont()->getColor()->setRGB('FFFFFF');

		$excel->getActiveSheet()->setShowGridlines(false);

		$filename = 'Meter_Tracking_Report_'. date('Ymd_his') . '.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		$objWriter->save('php://output');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("assets/tmp/" . $filename);
		//$this->index();
		// $data = ob_get_contents();
		//$tester =  $objWriter->save($filename);
		//force_download($filename,$objWriter->save('php://output')));
	}

	public function gen_pdf() {	
		$data = array('account_number'	=>	$_GET['account_number'],
					  'account_name'	=>	$_GET['account_name'],
					  'install_date'	=>	$_GET['install_date'],
					  'meter_serial'	=>	$_GET['meter_serial'],
					  'area'			=>	$_GET['area'],
					  'month_used'		=>	$_GET['month_used']);
		$q = $this->meter_tracking_model->getData($data);
		$table_data = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		foreach ($q as $key) {
			$table_data .= '<tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->meaning . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;">' . $key->account_name . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->serial_number . ' </td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->install_date . '</td>
								<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->month_used . '</td></tr>';
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '></td>
			        <td style='text-align: center;width=33%; padding-top:-5px;'>
						<h4>Meter Tracking Report</h4>
					</td>
					<td style='text-align: center;width=33%;'><hr style='height: .3mm; background: #000; '>     </td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: left;width=15%; padding: 5px;'>Area</td>
					<td style='border-bottom:1px solid; text-align: left;width=20%; padding: 5px;'>Customer</td>
					<td style='border-bottom:1px solid; text-align: right;width=20%; padding: 5px;'>Meter Serial</td>
					<td style='border-bottom:1px solid; text-align: right;width=25%; padding: 5px;'>Installed Date</td>
					<td style='border-bottom:1px solid; text-align: right;width=20%; padding: 5px;'>Months Used</td>
				</tr>" .
			$table_data
			. "</table>
		</page>
		";
		require_once dirname(__FILE__).'\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Meter Tracking Report');
		$html2pdf->WriteHTML($content);
		$html2pdf->Output('Meter_Tracking_Report_' . date("dmY_His") . '.pdf', 'D');
	}

}